
package DAO;

import DTO.Category;
import Exception.KwickKioskDBException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;

/**
 * This class represents abstraction for data access from category table in database.
 * This class is called from DAO as a proxy.
 * @see DAO
 * @Date Mar 30, 2011
 * @author Tony Lei
 */
public class CategoryDAO {

    private Connection conn = null;
    private static final Logger logger = Logger.getLogger(
            ItemDAO.class.getName());

    /**
     * constructor for this class.
     * @param conn connection object
     */
    public CategoryDAO(Connection conn) {
        this.conn = conn;
    }

    /**
     * This method retrieves all the categories of products from database
     * @return a list of categories
     * @throws {@link KwickKioskDBException}
     */
    public ArrayList<Category> getCategories() throws KwickKioskDBException {
        ArrayList<Category> list = new ArrayList<Category>();
        Statement sqlStatement;
        try {
            sqlStatement = conn.createStatement();
            String sqlText = "select * " + "from category c";
            ResultSet rs = sqlStatement.executeQuery(sqlText);
            while (rs.next()) {
                list.add(new Category(rs.getInt(1), rs.getInt(3),
                        rs.getString(2)));
            }
            //       return list;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "CategoryDAO : getCategoryName" + ex);
            throw new KwickKioskDBException("CategoryDAO: getCategoryName",
                    ex.getMessage());
        }
        return list;
    }
}
